package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        baseMapper.insert(operLog);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        LambdaQueryWrapper<SysOperLog> wrapper = Wrappers.lambdaQuery();

        // 构建查询条件
        if (operLog != null) {
            // IP地址模糊查询
            if (operLog.getOperIp() != null && !operLog.getOperIp().isEmpty()) {
                wrapper.like(SysOperLog::getOperIp, operLog.getOperIp());
            }

            // 标题模糊查询
            if (operLog.getTitle() != null && !operLog.getTitle().isEmpty()) {
                wrapper.like(SysOperLog::getTitle, operLog.getTitle());
            }

            // 业务类型查询
            if (operLog.getBusinessType() != null) {
                wrapper.eq(SysOperLog::getBusinessType, operLog.getBusinessType());
            }

            // 状态查询
            if (operLog.getStatus() != null) {
                wrapper.eq(SysOperLog::getStatus, operLog.getStatus());
            }

            // 操作人员模糊查询
            if (operLog.getOperName() != null && !operLog.getOperName().isEmpty()) {
                wrapper.like(SysOperLog::getOperName, operLog.getOperName());
            }

            // 时间范围查询
            if (operLog.getParams() != null) {
                Map<String, Object> params = operLog.getParams();
                if (params.get("beginTime") != null) {
                    wrapper.ge(SysOperLog::getOperTime, params.get("beginTime"));
                }
                if (params.get("endTime") != null) {
                    wrapper.le(SysOperLog::getOperTime, params.get("endTime"));
                }
            }
        }

        // 按操作ID倒序排列
        wrapper.orderByDesc(SysOperLog::getOperId);

        return baseMapper.selectList(wrapper);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        if (operIds == null || operIds.length == 0) {
            return 0;
        }
        return baseMapper.deleteBatchIds(Arrays.asList(operIds));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return baseMapper.selectById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        // 使用 LambdaQueryWrapper 删除所有记录
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        // 使用一个永远为真的条件来避免全表删除拦截
        wrapper.isNotNull(SysOperLog::getOperId);
        remove(wrapper);
    }
}
